package com.haiyi.software.entity;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

import com.haiyi.software.app.MainApplication;
import com.haiyi.software.dao.ChatDAO;

import android.R.integer;
import android.content.Context;
import android.util.Log;

/**
 * 基本消息类
 * 
 * @author Administrator
 * 
 */
public abstract class EntityMsg {

	protected EntityMsg() {

	}

	public final static int MESSAGECATEGORY_VOICE = 1;
	public final static int MESSAGECATEGORY_TEXT = 2;
	public final static int MESSAGECATEGORY_VIDEO = 3;
	public final static int MESSAGECATEGORY_GIF = 4;
	public final static int MESSAGECATEGORY_IMAGE = 5;

	protected String body;
	protected String allMessage;
	protected String username;
	protected String uhead;
	protected String messageID;
	protected String yUID;
	protected String name;
	protected String datetime;
	protected UserFriend userFriend;

	protected Context context;

	public void setContext(Context context) {
		this.context = context;
	}

	protected ChatDAO dao = ChatDAO.getInstence();

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getAllMessage() {
		return allMessage;
	}

	public abstract void setAllMessage(String allMessage);

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUhead() {
		return uhead;
	}

	public void setUhead(String uhead) {
		this.uhead = uhead;
	}

	public String getMessageID() {
		return messageID;
	}

	public void setMessageID(String messageID) {
		this.messageID = messageID;
	}

	public UserFriend getUserFriend() {
		return userFriend;
	}

	public void setUserFriend(UserFriend userFriend) {
		this.userFriend = userFriend;
	}

	public String getDatetime() {
		return datetime;
	}

	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getyUID() {
		return yUID;
	}

	public void setyUID(String yUID) {
		this.yUID = yUID;
	}

	public abstract Message getPacket();

	public abstract int getMessageCategory();

	public void setMessageFromChat(EntityChat chat) {

		String str_XML = chat.getBody();

		this.setyUID(chat.getHi_id());

		datetime = chat.getTimes();

		setAllMessage(str_XML);
	}

	/**
	 * 把消息存放在数据库中
	 * 
	 * @param message
	 * @param uf
	 */
	protected void AddMyMessageToDB(EntityMsg message, UserFriend uf) {

		EntityChat chat = new EntityChat();

		chat.setBody(message.getPacket().toXML());

		chat.setDownload("1");

		chat.setRead_state("1");// 我自己发的消息，永远是已读

		chat.setHi_id(uf.getUfID());

		chat.setName(uf.getUfName());

		chat.setSiID(message.getPacket().getPacketID());

		chat.setState("1");

		chat.setUhead(uf.getUfPhoto());

		chat.setTimes(message.getDatetime());

		dao.Add(chat);
	}

	protected void sendXMPPMessage(EntityMsg entityMsg) {
		XMPPConnection connection = MainApplication.getInstance()
				.getmXMPPStream().GetXMPPConnection();

		connection.sendPacket(entityMsg.getPacket());
	}
}
